﻿using System;
using System.ComponentModel;
using System.Drawing;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.DB;
using Autodesk.Revit.UI;
using YArchitech.LIB;
using YArchitech.MEP.Electric.Common;

namespace HYRElectric.WirePlace
{
	public partial class DlgPanelOutLine : System.Windows.Forms.Form
	{
		public DlgPanelOutLine(Document doc)
		{
			this.m_Doc = doc;
			this.InitializeComponent();
			this.nudWireNum.TextChanged += this.nudWireNum_TextChanged;
			this.nudWireLength.TextChanged += this.nudWireLength_TextChanged;
			this.nudSpecifyDistance.TextChanged += this.nudSpecifyDistance_TextChanged;
		}

		private void nudSpecifyDistance_TextChanged(object sender, EventArgs e)
		{
            System.Windows.Forms.Control control = sender as System.Windows.Forms.Control;
			if (string.IsNullOrEmpty(control.Text))
			{
				TaskDialog.Show("盈建科提示", "出线间距不能为空");
				control.Text = "200";
			}
		}

		private void nudWireLength_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.Control control = sender as System.Windows.Forms.Control;
			if (string.IsNullOrEmpty(control.Text))
			{
				TaskDialog.Show("盈建科提示", "出线长度不能为空");
				control.Text = "500";
			}
		}

		private void PanelOutLine_Load(object sender, EventArgs e)
		{
			//base.Icon = Resource.YArchitechIcon;
			this.xmlHelper.LoadXml(this.path, "Root");
			this.LoadDlgData();
		}

		private void nudWireNum_TextChanged(object sender, EventArgs e)
		{
			System.Windows.Forms.Control control = sender as System.Windows.Forms.Control;
			if (string.IsNullOrEmpty(control.Text))
			{
				TaskDialog.Show("盈建科提示", "出线数量不能为空");
				control.Text = "1";
			}
		}

		private void LoadDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYPanelOutLine", "Params"));
			string attribute = this.xmlHelper.GetAttribute("WireNum");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.nudWireNum.Value = Convert.ToInt32(attribute);
			}
			attribute = this.xmlHelper.GetAttribute("WireLength");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.nudWireLength.Value = Convert.ToInt32(attribute);
			}
			attribute = this.xmlHelper.GetAttribute("WireDistance");
			if (!string.IsNullOrEmpty(attribute))
			{
				this.nudSpecifyDistance.Value = Convert.ToInt32(attribute);
			}
			attribute = this.xmlHelper.GetAttribute("SelDistance");
			if (!string.IsNullOrEmpty(attribute))
			{
				if (attribute == "0")
				{
					this.rdoSpecifyDistance.Checked = true;
					return;
				}
				if (attribute == "1")
				{
					this.rdoAverageDistance.Checked = true;
				}
			}
		}

		private void SaveDlgData()
		{
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYPanelOutLine"));
			this.xmlHelper.SelectNode(Path.Combine("Root", "HYPanelOutLine", "Params"));
			this.xmlHelper.SetAttribute("WireNum", this.nudWireNum.Text);
			this.xmlHelper.SetAttribute("WireLength", this.nudWireLength.Text);
			this.xmlHelper.SetAttribute("WireDistance", this.nudSpecifyDistance.Text);
			string value;
			if (this.rdoSpecifyDistance.Checked)
			{
				value = "0";
			}
			else
			{
				value = "1";
			}
			this.xmlHelper.SetAttribute("SelDistance", value);
			this.xmlHelper.SaveXml("");
		}

		private void rdoSpecifyDistance_CheckedChanged(object sender, EventArgs e)
		{
			if (this.rdoSpecifyDistance.Checked)
			{
				this.nudSpecifyDistance.Enabled = true;
				return;
			}
			this.nudSpecifyDistance.Enabled = false;
		}

		private void DlgPanelOutLine_FormClosing(object sender, FormClosingEventArgs e)
		{
			if (this.m_WireSettingForm == null)
			{
				this.m_WireSettingForm = new NewWireParamForm(this.m_Doc);
				this.m_WireSettingForm.InitializeDefaultParameters();
			}
            if (((System.Windows.Forms.Form)sender).DialogResult == DialogResult.OK)
			{
				this.SaveDlgData();
			}
		}

		private void btnWireSetting_Click(object sender, EventArgs e)
		{
			this.m_WireSettingForm = new NewWireParamForm(this.m_Doc);
			this.m_WireSettingForm.ShowDialog();
		}

		private YArchitech.MEP.Electric.Common.XmlHelper xmlHelper = new YArchitech.MEP.Electric.Common.XmlHelper();

		private string path = Path.Combine(new string[]
		{
			Product.WorkingLocation + "PanelOutLine.xml"
		});

		private const string Root = "Root";

		private const string Node = "HYPanelOutLine";

		private const string ChildNode = "Params";

		private const string WireNum = "WireNum";

		private const string WireLength = "WireLength";

		private const string WireDistance = "WireDistance";

		private const string SelDistance = "SelDistance";

		private Document m_Doc;

		private NewWireParamForm m_WireSettingForm;
	}
}
